using System;
using Boo.Lang.Compiler;
using Boo.Lang.Compiler.Ast;
using Comdiv.Extensions;
using Comdiv.Model;
using Comdiv.Model.Interfaces;
using Comdiv.Logging;
using Comdiv.Application;
using Comdiv.Persistence;
using Comdiv.Inversion;
using Comdiv.Security.Acl;
using Comdiv.Conversations;
using Comdiv.IO;
using Comdiv.Security;
using System.Linq;


namespace wikiviewer.Controllers{
    public class WikiMacro : LexicalInfoPreservingMacro
    {
        private static int idx = 1;
        protected override Statement ExpandImpl(MacroStatement macro)
        {
            if (macro.Arguments.Count == 0)
            {
                throw new Exception("init must be called with argument");
            }
            var regex = (StringLiteralExpression)macro.Arguments[0];
            var replace = (StringLiteralExpression)macro.Arguments[1];
            int i = idx++;
            var call = new MethodInvocationExpression(AstUtil.CreateReferenceExpression("RegexWikiFormatter"), regex, replace,new IntegerLiteralExpression(i));
            var name = "wikiregex:" + i;
            var registryref = new SlicingExpression(new ReferenceExpression("registry"), new StringLiteralExpression(name));
            var result = new Block();
            result.Add(new ExpressionStatement(
                           new BinaryExpression(BinaryOperatorType.Assign,
                                                registryref, call
                               )
                           ));
            return result;
        }
    }
}